<?php

namespace App\Http\Controllers\Manage;


use App\Models\Course;
use App\Models\CourseCycle;
use App\Models\SiteInfo;
use App\Models\User;

class CourseCycleController extends BaseController
{

    // 课程上课时间
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseCycle';

        $opt = $this->optLogic();
        $_id = $opt->getData('course_id', 0, 'intval');
        $opt->assign('init_url', Url('/manage/course-cycle/lists?is_pop=1&course_id='.$_id));
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 活动信息
        $cids    = array_column($lists, 'course_id');
        $courses = (new Course())->isCache(true)->getListInIds($cids);
        $cycles  = (new CourseCycle())->getCycles();

        // 展示列表表格
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id = $item['id'];
            //$status   = $item['status'] == 1 ? '正常' : '禁用';
            //$statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            //$operate .= $listHtml->operate_edit(['id' => $_id], '编辑课时');
            $operate .= $listHtml->operate_delete(['id' => $_id], '你确定要删除该课时吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['id']);
            $tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($courses[$item['course_id']]['name'] ?? '-');
            $tr .= $listHtml->list_td($cycles[$item['cycle']] ?? '-');
            $tr .= $listHtml->list_td($item['number']);
            $tr .= $listHtml->list_date($item['start_time'] - 8 * 3600, 'time');
            //$tr .= $listHtml->list_date($item['end_time'] - 8 * 3600, 'time');

            //$tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl = Url('/manage/course-cycle/add');

        $c_id   = $this->optLogic()->getData('course_id', 0, 'intval');
        $addUrl = Url('/manage/course-cycle/add?course_id='.$c_id);
        $delUrl = Url('/manage/course-cycle/batch-delete?course_id='.$c_id);

        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加课程的周期</a> ';
        //$this->assign('top_button', $top_button);
        $this->assign('top_button', '');
    }

    // 列表字段
    protected function list_fields()
    {
        return ['id' => 'ID', 'uid' => '用户名', 'course_id' => '课程', 'cycle' => '周期', 'number' => '第几天/周几', 'start_time' => '开始时间', 'ctime' => '创建时间', 'operate' => '操作',];  // 'end_time' => '结束时间',
    }

    // 列表搜索
    protected function list_search()
    {
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();

        //$course_id  = $form->formInput('course_id', $request['course_id'] ?? '', '课程名');
        //$student_id = $form->formInput('student_id', $request['student_id'] ?? '', '用户名');
        //$uid        = $form->ajaxSingleFromList('uid', '用户ID', 'User', 'id', 'username');
        $course_id  = $form->ajaxSingleFromList('course_id', '课程ID', 'Course', 'id', 'name');
        $student_id = $form->ajaxSingleFromList('student_id', '学员ID', 'Student', 'id', 'person_name');

        $search = ['course_id' => $course_id,];  // 'username' => $username,
        //$this->assign('search', $search);
        $this->assign('search', []);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();
        $c_id = $this->optLogic()->getData('course_id', 0, 'intval');

        // 获取用户信息
        $uInfo  = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $cInfo  = (new Course())->isCache(true)->getOneById($info['course_id'] ?? $c_id);
        $cycles = (new CourseCycle())->getCycles();

        // 时间戳 相差八个小时
        if($info){
            $info['start_time'] = $info['start_time'] - 8 * 3600;
            $info['end_time']   = $info['end_time'] - 8 * 3600;
        }

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        //$status      = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        $uid        = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $auth_id    = $form->ajaxSingleFromList('course_id', $cInfo, 'Course', 'id', 'name');
        $cycle      = $form->formInput('cycle', $info['cycle'] ?? '', '周期');
        $cycle      = $form->formSelect('cycle', $info['cycle'] ?? '', $cycles);
        $times      = $form->formNumber('number', $info['number'] ?? '', '第几天/周几');
        $start_time = $form->formDateTime('start_time', $info['start_time'] ?? '', '开始时间', 'time');
        $end_time   = $form->formDateTime('end_time', $info['end_time'] ?? '', '结束时间', 'time');
        $course_id  = $form->ajaxSingleFromList('course_id', $cInfo, 'course', 'id', 'name');
        //$course_id  = $form->formHidden('course_id', $info['course_id'] ?? $c_id);
        $_id = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        //$html .= $form->makeItem('用户名', $uid);
        //$html .= $form->makeItem('认证名', $auth_id);
        $html .= $form->makeItem('课程名', $course_id);
        $html .= $form->makeItem('周期', $cycle);
        $html .= $form->makeItem('第几天/周几', $times);
        $html .= $form->makeItem('开始时间', $start_time);
        //$html .= $form->makeItem('结束时间', $end_time);
        //$html .= $form->makeItem('状态', $status);
        //$html .= $course_id;
        $html .= $_id;

        return $html;
    }


}


